package com.nuoniu.sibanyun.controller.system;


import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.SysDictType;
import com.nuoniu.sibanyun.entity.dto.DictTypeSaveDto;
import com.nuoniu.sibanyun.entity.dto.DictTypeSearchDto;
import com.nuoniu.sibanyun.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-05-25
 */
@RestController
@RequestMapping("/dictType")
@Api(tags = "字典类型表")
public class SysDictTypeController extends BaseController {

    @Autowired
    private ISysDictTypeService sysDictTypeService;

    @ApiOperation("获取字典类型列表")
    @GetMapping("/system/page")
    public HttpResult page(DictTypeSearchDto dictTypeSearchDto){
        return HttpResult.ok(sysDictTypeService.page(dictTypeSearchDto));
    }

    @ApiOperation("保存字典类型")
    @PostMapping("/system/saveDictType")
    public HttpResult saveDictType(@RequestBody DictTypeSaveDto dictTypeSaveDto){
        if(StringUtils.isBlank(dictTypeSaveDto.getDictCode())) {
            throw new NuoNiuException("字典编码不能为空");
        }

        return HttpResult.ok(sysDictTypeService.saveOrUpdate(dictTypeSaveDto,request));
    }

    @ApiOperation("保存排序")
    @PostMapping("saveSort")
    public HttpResult saveSort(@RequestBody List<SysDictType> dictTypes) {
        sysDictTypeService.updateBatchById(dictTypes);
        return HttpResult.ok();
    }

    @PutMapping("/system/disable")
    public HttpResult disable(Long id){
        return HttpResult.ok(sysDictTypeService.updateStatus(id,2,request));
    }

    @PutMapping("/system/enable")
    public HttpResult enable(Long id){
        return HttpResult.ok(sysDictTypeService.updateStatus(id,0,request));
    }

    @DeleteMapping("/system/delete")
    public HttpResult delete(Long id){
        return HttpResult.ok(sysDictTypeService.delete(id,request));
    }

    @ApiOperation("获取所有字典数据")
    @GetMapping("getDictDataAll")
    public HttpResult<DictTypeSaveDto> getDictDataAll() {
        return HttpResult.ok(sysDictTypeService.getDictDataAll());
    }

}
